mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
range.h File Reference

More...

#include "common.h"
Include dependency graph for range.h:

Go to the source code of this file.

Classes

struct  mrb_range_edges
 Range class. More...
struct  RRange

Macros

#define mrb_gc_free_range(mrb, p)
#define RANGE_BEG(p)
#define RANGE_END(p)
#define mrb_range_beg(mrb, r)
#define mrb_range_end(mrb, r)
#define mrb_range_excl_p(mrb, r)
#define mrb_range_raw_ptr(r)
#define mrb_range_value(p)
#define RANGE_EXCL(p)

Typedefs

typedef struct mrb_range_edges mrb_range_edges
 Range class.

Enumerations

enum  mrb_range_beg_len { MRB_RANGE_TYPE_MISMATCH = 0 , MRB_RANGE_OK = 1 , MRB_RANGE_OUT = 2 }

Functions

struct RRangemrb_range_ptr (mrb_state *mrb, mrb_value range)
 Retrieves a pointer to the internal RRange structure from a Range object.
mrb_value mrb_range_new (mrb_state *mrb, mrb_value start, mrb_value end, mrb_bool exclude)
 Creates a new Range object.
enum mrb_range_beg_len mrb_range_beg_len (mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc)
 Calculates the beginning position and length of a range.

Detailed Description

  • Range class

See Copyright Notice in mruby.h

Macro Definition Documentation

◆ mrb_gc_free_range

#define mrb_gc_free_range ( mrb,
p )
Value:
mrb_free(mrb, (p)->edges)

◆ mrb_range_beg

#define mrb_range_beg ( mrb,
r )
Value:
RANGE_BEG(mrb_range_ptr(mrb, r))

◆ mrb_range_end

#define mrb_range_end ( mrb,
r )
Value:
RANGE_END(mrb_range_ptr(mrb, r))

◆ mrb_range_excl_p

#define mrb_range_excl_p ( mrb,
r )
Value:
RANGE_EXCL(mrb_range_ptr(mrb, r))

◆ mrb_range_raw_ptr

#define mrb_range_raw_ptr ( r)
Value:
((struct RRange*)mrb_ptr(r))
Definition range.h:36

◆ mrb_range_value

#define mrb_range_value ( p)
Value:
mrb_obj_value((void*)(p))

◆ RANGE_BEG

#define RANGE_BEG ( p)
Value:
((p)->edges->beg)

◆ RANGE_END

#define RANGE_END ( p)
Value:
((p)->edges->end)

◆ RANGE_EXCL

#define RANGE_EXCL ( p)
Value:
((p)->excl)

Function Documentation

◆ mrb_range_beg_len()

enum mrb_range_beg_len mrb_range_beg_len ( mrb_state * mrb,
mrb_value range,
mrb_int * begp,
mrb_int * lenp,
mrb_int len,
mrb_bool trunc )
extern

Calculates the beginning position and length of a range.

This function is typically used to get array offsets. It interprets the range as a sequence of integers.

  • If the beginning of the range is nil, it's treated as 0.
  • If the end of the range is nil, it's treated as -1 (or the last element).

The function handles negative indices, out-of-bounds conditions, and truncation based on the provided length and trunc flag.

Parameters
mrbThe mruby state.
rangeThe Range object (mrb_value).
[out]begpPointer to store the calculated beginning position.
[out]lenpPointer to store the calculated length.
lenThe length of the sequence the range is being applied to.
truncIf true, truncate the range to fit within the given length.
Returns
An enum mrb_range_beg_len indicating success (MRB_RANGE_OK), type mismatch (MRB_RANGE_TYPE_MISMATCH), or out of bounds (MRB_RANGE_OUT).

◆ mrb_range_new()

mrb_value mrb_range_new ( mrb_state * mrb,
mrb_value beg,
mrb_value end,
mrb_bool excl )
extern

Creates a new Range object.

This function allocates and initializes a new Range object with the given beginning and end values, and an exclude_end flag.

Parameters
mrbThe mruby state.
begThe beginning value of the range.
endThe end value of the range.
exclA boolean flag indicating whether the end value is excluded (true) or included (false).
Returns
The new Range object (mrb_value).

◆ mrb_range_ptr()

struct RRange * mrb_range_ptr ( mrb_state * mrb,
mrb_value range )
extern

Retrieves a pointer to the internal RRange structure from a Range object.

This function ensures that the given Range object is properly initialized before returning a pointer to its data. If the Range is not initialized, it raises an E_ARGUMENT_ERROR.

Parameters
mrbThe mruby state.
rangeThe Range object (mrb_value) from which to get the pointer.
Returns
A pointer to the struct RRange.